//
//  HYServerCell.m
//  HaiYue
//
//  Created by skyline on 2017/12/20.
//  Copyright © 2017年 skyline. All rights reserved.
//

#import "HYServerCell.h"
@interface HYServerCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UIView *timeView;
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation HYServerCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifierP
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifierP];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HYOnlineBarCellHeight) collectionViewLayout:[self layout]];
        [_collectionView registerClass:[HYServerCollectionCell class] forCellWithReuseIdentifier:@"HYServerCollectionCell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.bottom.equalTo(self);
        }];
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layout
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 2.0,HYOnlineBarCellHeight);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    return layout;
}
- (CGFloat)H
{
    return (self.arr.count % 2)?((self.arr.count / 2 + 1) * HYOnlineBarCellHeight):(self.arr.count / 2 * HYOnlineBarCellHeight);
}
- (void)setArr:(NSMutableArray *)arr
{
    if (_arr != arr)
    {
        _arr = arr;
        self.collectionView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.H);
        [self.collectionView reloadData];
    }
}
#pragma mark CollectionViewDelegate&DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellId = @"HYServerCollectionCell";
    HYServerCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    if (!cell)
    {
        cell = [[HYServerCollectionCell alloc] init];
    }
    cell.model = self.arr[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
